# -*- coding: utf-8 -*-
""" Auteur: Eliott """
from Tkinter import *
import TextualAnalysis
server = None

""" Cette classe sert à gérer l'interface pour gérer les liens entre les différents
    mot-clés.
    Il est possible d'associer des adjectifs et des verbes à des noms pour nous aider ensuite
    à concevoir des fiches (CRC).
    Responsabilités:
        addVerbs()
        removeVerbs()
        addAdj()
        removeAdj()
        windowClosed()
        findKeyword(name)
        selectKeyword(event)
        updateKeywordsLists()
    Collaborateurs:
        Liste des mot-clé
        Le client
        Objets Tkinter associés à la fenêtre
"""
class InterfaceKeyWord:
    def __init__(self,client):
        self.client = client
        self.window = Toplevel(client.root)
        self.window.protocol("WM_DELETE_WINDOW", self.windowClosed)
        
        self.keywords = self.client.server.getObjectsOfClass(TextualAnalysis.KeyWord)
        
        self.nounsListbox = Listbox(self.window)
        self.nounsListbox.grid(column=1, row=0)
        self.nounsListbox.bind("<Double-Button-1>", self.selectKeyword)
        
        self.selectedKeywordString = StringVar()
        self.selectedKeyword = None
        Label(self.window, textvariable=self.selectedKeywordString).grid(column=1, row=1)
        Label(self.window, text="Liens de verbes").grid(column=0,row=1)
        Label(self.window, text="Verbes").grid(column=2,row=1)
        Label(self.window, text="Liens d'adjectifs").grid(column=0,row=4, sticky=N)
        Label(self.window, text="Adjectifs").grid(column=2,row=4, sticky=N)
        
        self.associatedVerbs = Listbox(self.window, selectmode=EXTENDED)
        self.associatedVerbs.grid(column=0, row=2, rowspan=2, sticky=W)
        self.verbsListbox = Listbox(self.window, selectmode=EXTENDED)
        self.verbsListbox.grid(column=2, row=2, rowspan=2, sticky=E)
        
        Button(self.window, text="<<", command=self.addVerbs).grid(column=1, row=2, sticky=S)
        Button(self.window, text=">>", command=self.removeVerbs).grid(column=1, row=3, sticky=N)
        

        
        self.associatedAdjectives = Listbox(self.window, selectmode=EXTENDED)
        self.associatedAdjectives.grid(column=0,row=5,rowspan=2,sticky=W)
        self.adjectivesListbox = Listbox(self.window, selectmode=EXTENDED)
        self.adjectivesListbox.grid(column=2,row=5,rowspan=4,sticky=E)
        
        Button(self.window, text="<<", command=self.addAdj).grid(column=1, row=4, sticky=S)
        Button(self.window, text=">>", command=self.removeAdj).grid(column=1, row=5, sticky=N)
        
        self.nouns = set(filter(lambda kw: kw.type == TextualAnalysis.NOUN, self.keywords))
        self.adjectives = set(filter(lambda kw: kw.type == TextualAnalysis.ADJECTIVE, self.keywords))
        self.verbs = set(filter(lambda kw: kw.type == TextualAnalysis.VERB, self.keywords))
        
        for noun in filter(lambda kw: kw.type == TextualAnalysis.NOUN, self.keywords):
            self.nounsListbox.insert(END, noun)
            
    def addVerbs(self):
        theKeyword = self.findKeyword(self.selectedKeywordString.get())
        theKeyword.addLink(self.findKeyword(self.verbsListbox.get(self.verbsListbox.index(ACTIVE))))
        self.associatedVerbs.insert(END,self.verbsListbox.get(self.verbsListbox.index(ACTIVE)))
        self.verbsListbox.delete(self.verbsListbox.index(ACTIVE))
        
    def removeVerbs(self):
        theKeyword = self.findKeyword(self.selectedKeywordString.get())
        theKeyword.deleteLink(self.findKeyword(self.associatedVerbs.get(self.associatedVerbs.index(ACTIVE))))   
        self.verbsListbox.insert(END,self.associatedVerbs.get(self.associatedVerbs.index(ACTIVE)))
        self.associatedVerbs.delete(self.associatedVerbs.index(ACTIVE))
              
        
               
    def addAdj(self):
        theKeyword = self.findKeyword(self.selectedKeywordString.get())
        theKeyword.addLink(self.findKeyword(self.adjectivesListbox.get(self.adjectivesListbox.index(ACTIVE))))
        self.associatedAdjectives.insert(END,self.adjectivesListbox.get(self.adjectivesListbox.index(ACTIVE)))
        self.adjectivesListbox.delete(self.adjectivesListbox.index(ACTIVE))
        
    def removeAdj(self):
        theKeyword = self.findKeyword(self.selectedKeywordString.get())
        theKeyword.deleteLink(self.findKeyword(self.associatedAdjectives.get(self.associatedAdjectives.index(ACTIVE))))
        self.adjectivesListbox.insert(END,self.associatedAdjectives.get(self.associatedAdjectives.index(ACTIVE)))
        self.associatedAdjectives.delete(self.associatedAdjectives.index(ACTIVE))       
     
    def windowClosed(self):
        for KeyWord in self.keywords:
            KeyWord = client.server.setObject(KeyWord)
        self.window.destroy()
    
    def findKeyword(self,name):
        for kw in self.keywords:
            if kw.keyWord == name:
                return kw
        
    
    def selectKeyword(self, event):
        noun = [keyword for keyword in self.nouns if keyword.keyWord == self.nounsListbox.get(ACTIVE)][0]
        self.selectedKeyword = noun
        self.selectedKeywordString.set(noun)
        self.updateKeywordsLists()
        for KeyWord in self.keywords:
            KeyWord = client.server.setObject(KeyWord)
    
    def updateKeywordsLists(self):
        adjectives = set(filter(lambda kw: kw.type == TextualAnalysis.ADJECTIVE, self.selectedKeyword.linkedKeyWords))
        verbs = set(filter(lambda kw: kw.type == TextualAnalysis.VERB, self.selectedKeyword.linkedKeyWords))
        
        self.associatedAdjectives.delete(0, END)
        self.associatedVerbs.delete(0,END)
        self.adjectivesListbox.delete(0, END)
        self.verbsListbox.delete(0, END)
        for adjective in adjectives-self.adjectives:
            self.associatedAdjectives.insert(END,adjective)
        for verb in verbs-self.verbs:
            self.associatedVerbs.insert(END,verb)   
        
        for adjective in self.adjectives-adjectives:
            self.adjectivesListbox.insert(END,adjective)
        for verb in self.verbs-verbs:
            self.verbsListbox.insert(END,verb)
            

def PlugIn(client_):
    global client
    client = client_
    bouttonMenu = Button(client.frame,text="Key Words",command=callInterfaceKeyWord)
    bouttonMenu.pack()
    
def callInterfaceKeyWord():
    InterfaceKeyWord(client)